package com.belf.evaluatetool;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;

@SpringBootApplication
public class EvaluateToolApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(EvaluateToolApplication.class, args);
        System.out.println("evaluateTool start success");
    }

    @Override
    public void run(String... args) throws Exception {

        // 指定要访问的路径
        String path = "/home.html";
        // 拼接完整的 URL
        String url = "http://localhost:" + 9123 + path;

        // 打开默认浏览器并访问指定 URL
        openBrowser(url);
    }

    private void openBrowser(String url) {
        try {
            // 根据操作系统执行不同的命令
            String os = System.getProperty("os.name").toLowerCase();
            if (os.contains("win")) {
                // Windows
                new ProcessBuilder("cmd.exe", "/c", "start", url).start();
            } else if (os.contains("mac")) {
                // MacOS
                new ProcessBuilder("open", url).start();
            } else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) {
                // Unix/Linux
                new ProcessBuilder("xdg-open", url).start();
            } else {
                throw new UnsupportedOperationException("Unsupported OS: " + os);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
